class SimpleMapLoader
{
    //TODO
    //1、增加Icontrol加载
    //
    constructor(){
        this.layerTypes = {
            'fill': ['fill-opacity'],
            'line': ['line-opacity'],
            'circle': ['circle-opacity', 'circle-stroke-opacity'],
            'symbol': ['icon-opacity', 'text-opacity'],
            'raster': ['raster-opacity'],
            'fill-extrusion': ['fill-extrusion-opacity']
        };
    }

    getLayerPaintType(layer) {
        var layerType = this.map.getLayer(layer).type;
        return this.layerTypes[layerType];
    }

    setLayerOpacity(layer) {
        var paintProps = this.getLayerPaintType(layer.layer);
        paintProps.forEach(function (prop) {
            this.map.setPaintProperty(layer.layer, prop, layer.opacity);
        });
    }

    mountMapBook(mapbook)
    {
        this.mapbook = mapbook;
    }

    loadMap(mapContainer, config)
    {
        //初始化地图
        mapboxgl.accessToken = config.accessToken;
        this.mapbook.map = this.map = new mapboxgl.Map({
            container: Select.ClearPrefix(mapContainer),
            style: config.style,
            center: config.center.center,
            zoom: config.center.zoom,
            bearing: config.center.bearing,
            pitch: config.center.pitch,
            scrollZoom: config.scrollZoom,
            transformRequest: function (url) {
                var hasQuery = url.indexOf("?") !== -1;
                var suffix = hasQuery ? "&pluginName=history" : "?pluginName=history";
                return {
                    url: url + suffix
                };
            }
        });
        this.setMapLanguage(config.language);
        this.addControl(config.controls);
    }

    addControl(controlConfig)
    {
        if(controlConfig)
        {
            controlConfig.forEach(control => {
                if(control.name == "Navigation")
                {
                    this.map.addControl(new mapboxgl.NavigationControl(), control.position);
                }
                else if(control.name == "GeoLocate")
                {
                    this.map.addControl(new mapboxgl.GeolocateControl({
                        positionOptions: {
                        enableHighAccuracy: true
                        },
                        trackUserLocation: true
                        }), control.position);
                }
                else if(control.name == "Scale")
                {
                    this.map.addControl(new mapboxgl.ScaleControl(), control.position);
                }
                else if(control.name == "Fullscreen")
                {
                    this.map.addControl(new mapboxgl.FullscreenControl(), control.position);
                }
            });
        }
    }

    setMapLanguage(language)
    {
        var language = new MapboxLanguage({
            defaultLanguage: 'zh'
        });
        this.map.addControl(language);
    }
}